﻿@{
    // konekcija na bazu
    var db = Database.Open("CvProjectDatabase");

    var inputFirstName = Request.QueryString["firstName"];
    var inputLastName = Request.QueryString["LastName"];
    var inputPracticalScore = Request.QueryString["practicalScore"];
    var inputTheoreticalScore = Request.QueryString["theoreticalScore"];
    var inputNotes = Request.QueryString["notes"];
    var inputTags = Request.QueryString["name"];
    
    var IsDeleted = "False";
    var AppId = Request.QueryString["AppId"];

    var selectHistoryEntry = "SELECT Hid, A.AppId, CreationDate, Comment, A.isDeleted, Name, C.LastName, D.LastName, D.Username FROM HistoryEntry A, HistoryEntryType B, Applicants C, Users D WHERE A.IsDeleted = 'False' and A.HTypeId = B.HtypeId and A.AppId = C.AppId and A.UserId = D.UserId and C.LastName = @0";
    var historyEntry = db.Query(selectHistoryEntry, inputLastName);
    var deleteQuery = "UPDATE HistoryEntry SET IsDeleted = 'True' WHERE HId = @0";

    var selectAppDetails = "SELECT * FROM Applicants WHERE IsDeleted = 'False'";
    var appDetails = db.Query(selectAppDetails);

    var selectAppFiles = "SELECT * FROM ApplicantFiles WHERE IsDeleted = 'False' and AppId = @0";
    var appFiles = db.Query(selectAppFiles, AppId);

    var instertFile = "INSERT INTO ApplicantFiles (FileName, Format, Location, ID, IsDeleted, AppId) VALUES (@0, @1, @2, @3, @4, @5)";




    // tabela za prikaz History entry-a
    var gridHisEntry = new WebGrid(source: historyEntry, defaultSort: "HId", rowsPerPage:5);
    if(IsPost){
       historyEntry = db.Query(selectHistoryEntry, inputLastName);
       gridHisEntry = new WebGrid(source: historyEntry, defaultSort: "HId", rowsPerPage:5);
    }

   // tabela za prikaz fajlova
    var gridAppFiles = new WebGrid(source: appFiles, defaultSort: "FileId", rowsPerPage:5);
 /*   if(IsPost){
       appFiles = db.Query(selectAppFiles, AppId); 
        gridAppFiles = new WebGrid(source: appFiles, defaultSort: "FileId", rowsPerPage:5);
        }
*/
    // dodavanja fajla
   /* var fileName = "";
    if (IsPost) {
        var fileSavePath = "";
        var uploadedFile = Request.Files[0];
        fileName = Path.GetFileName(uploadedFile.FileName);
        fileSavePath = Server.MapPath("~/res/files/" + fileName);
        uploadedFile.SaveAs(fileSavePath);
    }*/


    // File Upload
    var fileName = "";
    var ext = "";
    int numOfFiles = Request.Files.Count;

    if(IsPost && numOfFiles > 0){
        var fileSavePath = "";
        var timestamp =DateTime.Now.Ticks;
            for(int i = 0; i < numOfFiles; i++){
                timestamp = DateTime.Now.Ticks;
                var uploadedFile = Request.Files[i];
                if(uploadedFile.ContentLength > 0 ){
                    fileName = Path.GetFileName(uploadedFile.FileName);
                    ext = Path.GetExtension(uploadedFile.FileName);

                   
                        fileSavePath = Server.MapPath("~/res/files/" + timestamp+ext);
                        db.Execute(instertFile, fileName, ext,"~/res/files/", timestamp, IsDeleted, AppId);
                
                uploadedFile.SaveAs(fileSavePath);
            } 
        }
        Response.Redirect("Details.cshtml?AppId=" + AppId +"&FirstName="+ inputFirstName + "&LastName="+ inputLastName + "&PracticalScore="+ inputPracticalScore + "&TheoreticalScore="+ inputTheoreticalScore + "&Notes="+ inputNotes + "&name="+ inputTags );
    }

     // brisanje History entry-a
    var delete = Request.QueryString["delete"];
    if(!delete.IsEmpty()){
        db.Execute(deleteQuery, delete);
        Response.Redirect("Details.cshtml?AppId=" + AppId +"&FirstName="+ inputFirstName + "&LastName="+ inputLastName + "&PracticalScore="+ inputPracticalScore + "&TheoreticalScore="+ inputTheoreticalScore + "&Notes="+ inputNotes + "&name="+ inputTags );
    }
/*
    var delete = Request.QueryString["delete"];
    if(!delete.IsEmpty()){
        isDeleted = "True";
        db.Execute(deleteQuery, isDeleted, delete);
        Response.Redirect("UserAdministration.cshtml");
    }
*/
   
       
}
    


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="res/css/bootstrap.min.css" rel="stylesheet">
    <link href="res/css/table.css" rel="stylesheet">
    <link href="res/css/tableStyles.css" rel="stylesheet">

        

  </head>
  <body>
   
       <nav class="navbar navbar-default" role="navigation">
            <div class="container-fluid">
                 <ul id="navigacija" class="nav navbar-nav">
                     <li style="margin-right: 30px"><a href="Index.cshtml"><b style="font-size: 1.7em">CV Database</b></a></li>
                     <li style="font-size: 1.2em"><a href="UserAdministration.cshtml">User Administration</a></li>
                     <li style="padding-top: 15px"> | </li>
                   
                    
                        <li class="dropdown" style="font-size: 1.2em">
                        
                            
                            <li style="font-size: 1.2em"><a href="index.cshtml?izlogujSe=true">Izlogujte se</a></li>
                        </ul>
                    </li>
                    
                 </ul>
            </div>
        </nav>
       
       <fieldset>
           <legend>Applicant info</legend>
           <input type="text" name="AppId" hidden value="@Request.QueryString["AppId"]"/>
           <p><label for="firstName">First name:</label>
                
                 <input type="text" name="firstName" readonly="True" value="@Request.QueryString["FirstName"]" @Validation.For("firstName")/>
                  <p class="validacija">
                 
                  </p>
              </p>

              <p><label for="lastName">Last name:</label>
                 <input type="text" name="lastName" readonly="True" value="@Request.QueryString["lastName"]" @Validation.For("lastName")/>
                  <p class="validacija">
                  
                      </p>
              </p>

              <p><label for="practicalScore">Practical score:</label>
                 <input type="text" name="practicalScore" readonly="True" value="@Request.QueryString["practicalScore"]" @Validation.For("practicalScore")/>
                  <p class="validacija">
                  
                      </p>
              </p>

              <p><label for="theoreticalScore">Theoretical score:</label>
                 <input type="text" name="theoreticalScore" readonly="True" value="@Request.QueryString["theoreticalScore"]"  @Validation.For("theoreticalScore")/>
                  <p class="validacija">
                 
                      </p>
              </p>
                
              <p><label for="notes">Notes:</label>
                 <input type="text" name="notes" readonly="True" value="@Request.QueryString["notes"]"  @Validation.For("notes")/>
                  <p class="validacija">
                  
                      </p>
              </p>

             <p><label for="tags">Tags:</label></p>
                      <textarea name="tags" rows="4" cols="29" readonly @Validation.For("tags")> @Request.QueryString["name"]</textarea>
                <p>
                    <p class="validacija">
                      
                        </p>
              </p>

      <legend>History entry</legend>
      @gridHisEntry.GetHtml(
                    tableStyle: "grid",
                    headerStyle: "head",
                    alternatingRowStyle: "alt",
                    columns: gridHisEntry.Columns(  
                    gridHisEntry.Column("CreationDate"),                   
                    gridHisEntry.Column("Name"),
                    gridHisEntry.Column("LastName"),
                    gridHisEntry.Column("Username"),
                    gridHisEntry.Column("Comment"),
                    gridHisEntry.Column(format: @<a href="~/HistoryEntry?edit=true&AppId=@item.AppId&HId=@item.HId&entryDate=@item.CreationDate&comment=@item.Comment">Edit</a>),
                    gridHisEntry.Column(format: @<a href="~/Details?delete=@item.HId&AppId=@AppId&firstName=@inputFirstName&LastName=@inputLastName&practicalScore=@inputPracticalScore&theoreticalScore=@inputTheoreticalScore&notes=@inputNotes&name=@inputTags" onclick ="return confirm('Are you sure you want to delete this history entry?')">Delete</a>)
                )
            )      
                                                                                                                                                                                                                          
       <p><a href="HistoryEntry.cshtml?AppId=@AppId">Add new</a></p>
      <legend>Files</legend>
           
      @gridAppFiles.GetHtml(
                    
                    tableStyle: "grid",
                    headerStyle: "head",
                    alternatingRowStyle: "alt",

                    columns: gridAppFiles.Columns(                    
                    gridAppFiles.Column("FileName"),           
                    gridAppFiles.Column(format: @<a href="#">Open</a>)                   
                )
            )     
        <p>Add new file</p>
         <label for="FileTitle">Enter file name:</label>  <input type="text" name="FileTitle" />
        @FileUpload.GetHtml(
            initialNumberOfFiles:1,
            allowMoreFilesToBeAdded:true,
            addText:"Add more files", 
            includeFormTag:true,
            uploadText:"Upload"
            )
        @if (IsPost) {
            <span>File uploaded!</span><br/>
    }
     </fieldset>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="res/js/bootstrap.min.js"></script>
  </body>
</html>